// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#ifndef ROTHAR_PEGTL_SRC_EXAMPLES_PEGTL_JSON_ERRORS_HPP
#define ROTHAR_PEGTL_SRC_EXAMPLES_PEGTL_JSON_ERRORS_HPP

#include <rothar/pegtl.hpp>
#include <rothar/pegtl/contrib/json.hpp>



namespace example
{
   // This file shows how to throw exceptions with
   // custom error messages for parse errors.

#if defined( __cpp_exceptions )

   // clang-format off
   template< typename > inline constexpr const char* error_message = nullptr;

   template<> inline constexpr auto error_message< rothar::json::text > = "no valid JSON";

   template<> inline constexpr auto error_message< rothar::json::next_array_element > = "expected value";
   template<> inline constexpr auto error_message< rothar::json::end_array > = "incomplete array, expected ']'";

   template<> inline constexpr auto error_message< rothar::json::name_separator > = "expected ':'";
   template<> inline constexpr auto error_message< rothar::json::member_value > = "expected value";
   template<> inline constexpr auto error_message< rothar::json::next_member > = "expected member";
   template<> inline constexpr auto error_message< rothar::json::end_object > = "incomplete object, expected '}'";

   template<> inline constexpr auto error_message< rothar::json::digits > = "expected at least one digit";
   template<> inline constexpr auto error_message< rothar::json::xdigit > = "incomplete universal character name";
   template<> inline constexpr auto error_message< rothar::json::escaped > = "unknown escape sequence";
   template<> inline constexpr auto error_message< rothar::json::char_ > = "invalid character in string";
   template<> inline constexpr auto error_message< rothar::json::string::content > = "unterminated string";
   template<> inline constexpr auto error_message< rothar::json::key::content > = "unterminated key";

   template<> inline constexpr auto error_message< rothar::eof > = "unexpected character after JSON value";
   // clang-format on

   // As must_if<> can not take error_message as a template parameter directly, we need to wrap it.
   struct error
   {
      template< typename Rule >
      static constexpr auto message = error_message< Rule >;
   };

   template< typename Rule >
   using control = rothar::must_if< error >::control< Rule >;

#else

   template< typename Rule >
   using control = rothar::normal< Rule >;

#endif

}  // namespace example

#endif
